<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>3D立方体Loading加载动画 - HTML5 CSS3</title>

    <style>
        html, body {
            margin: 0;
        }

        /*
         * 这里有2个方法实现
         * 1 是用下面这段 position: fixed; 一般都是用这个，对原来的内容无要求，缺点是IOS系统微信内置浏览器会遮挡方块。。。目测是微信bug，暂时无解
         * 2 是用 display: flex; 曲线救国，缺点是width和height必须对其窗口，如果html body有margin，
         *   就会顶到屏幕外面，出现滚动条，且主页内容会被loading-background顶出屏幕
         */
        #loading-background {
            background-color: #dedede;
            width: 100%;
            height: 100%;
            position: fixed;
        }

        #loading{
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
        }

        .base {
            height: 9rem;
            padding: 3rem;
            width: 9rem;
            transform: rotateX(45deg) rotateZ(45deg);
            transform-style: preserve-3d;
        }

        .cube,
        .cube:after,
        .cube:before {
            content: '';
            float: left;
            height: 3rem;
            position: absolute;
            width: 3rem;
            z-index: 99999;
        }

        .cube {
            background-color: #05afd1;
            position: relative;
            transform: translateZ(3em);
            transform-style: preserve-3d;
            transition: .25s;
            box-shadow: 13em 13em 1.5em rgba(0, 0, 0, 0.1);
            animation: anim 1s infinite;
        }

        .cube:after {
            background-color: #049dbc;
            transform: rotateX(-90deg) translateY(3em);
            transform-origin: 100% 100%;
        }

        .cube:before {
            background-color: #048ca7;
            transform: rotateY(90deg) translateX(3em);
            transform-origin: 100% 0;
        }

        .cube:nth-child(1) {
            animation-delay: 0.05s;
        }

        .cube:nth-child(2) {
            animation-delay: 0.1s;
        }

        .cube:nth-child(3) {
            animation-delay: 0.15s;
        }

        .cube:nth-child(4) {
            animation-delay: 0.2s;
        }

        .cube:nth-child(5) {
            animation-delay: 0.25s;
        }

        .cube:nth-child(6) {
            animation-delay: 0.3s;
        }

        .cube:nth-child(7) {
            animation-delay: 0.35s;
        }

        .cube:nth-child(8) {
            animation-delay: 0.4s;
        }

        .cube:nth-child(9) {
            animation-delay: 0.45s;
        }

        @keyframes anim {
            50% {
                transform: translateZ(0.5em);
            }
        }
    </style>

</head>
<body>
<!-- 本内容非原创 全部基于这个例子 仅做了适当微调 https://www.html5tricks.com/demo/css3-3d-cube-loading/index.html -->
<div id="loading-background" ref="loading-background">
    <div id="loading" ref="loading">
        <div class="base">
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
            <div class="cube"></div>
        </div>
    </div>
</div>
<script>
    const loading = document.querySelector('#loading');
    const background = document.querySelector('#loading-background');
    let opacity = 100;

    /** 渐隐动画 每一帧减少1%透明度 1秒60帧 100帧后消失 */
    function loadingFadeOut() {
        if (opacity > 0) {
            requestAnimationFrame(function () {
                opacity -= 2;
                loading.style.opacity = opacity / 100;
                loadingFadeOut()
            });
        } else {
            document.body.removeChild(background);
        }
    }

    // 模拟3秒后 执行加载完成
    setTimeout(function () {
        loadingFadeOut();
    }, 3000)
</script>
</body>
</html>
